import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class MyFileWriter {
	int img_width;
	int img_height;
	int img_bpp;
	FileInputStream fin;
	FileChannel fcin;
	FileOutputStream fout;
	FileChannel fcout;

	String in_file_name;
	String out_file_name;

	MyFileWriter(int w, int h, int b, String in_name, String out_name)
			throws FileNotFoundException {
		img_width = w;
		img_height = h;
		img_bpp = b;
		in_file_name = in_name;
		out_file_name = out_name;

		File file = new File(out_file_name);
		fout = new FileOutputStream(file, false);
		fcout = fout.getChannel();

	}
	
	void openInputFile() throws FileNotFoundException {
		fin = new FileInputStream(in_file_name);
		fcin = fin.getChannel();
	}
	
	void writeFrame(ByteBuffer frame) throws IOException {
		//System.out.println(frame.capacity());
		fcout.write(frame);
	}

	void writeRange(VideoRange range) throws IOException {
		//fcin.position(0);
		fcin.position((long)((long)range.startFrameID * (long)img_width * (long)img_height * (long)img_bpp));
		
		ByteBuffer buffer = ByteBuffer.allocateDirect(img_bpp * img_height * img_width);
		for (int i = range.startFrameID; i <= range.stopFrameID; i++) {
			buffer.clear();
			buffer.position(0);
			fcin.read(buffer);
			buffer.position(0);
			fcout.write(buffer);
		}
	}
}
